package com.fjp.heartguard.ui.login

import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.view.KeyEvent
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.widget.addTextChangedListener
import com.fjp.heartguard.R
import com.fjp.heartguard.app.App
import com.fjp.heartguard.app.Constants
import com.fjp.heartguard.app.base.BaseActivity
import com.fjp.heartguard.app.data.bean.UserInfo
import com.fjp.heartguard.app.ext.showToast
import com.fjp.heartguard.app.util.CacheUtil
import com.fjp.heartguard.databinding.ActivityVerificationCodeBinding
import com.fjp.heartguard.ui.main.MainActivity
import com.fjp.heartguard.viewmodel.VerificationCodeViewModel
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.ext.parseState

class VerificationCodeActivity : BaseActivity<VerificationCodeViewModel, ActivityVerificationCodeBinding>() {
     var mobile:String = ""
     lateinit var userInfo:UserInfo
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.viewModel = mViewModel
        mobile = intent.getStringExtra(Constants.INTENT_PHONE)!!
        userInfo = intent.getParcelableExtra<UserInfo>(Constants.INTENT_USER_INFO)!!
        mViewModel.sendPhone.set("验证码已发送至 +86 " + mobile)

        mViewModel.getCode(mobile)
        startCountDown()
        mDatabind.click = ClickProxy()
        mDatabind.et1.addTextChangedListener {
            if (it?.length != 0) {
                mDatabind.et2.requestFocus()
            }
        }
        mDatabind.et2.addTextChangedListener {
            if (it?.length != 0) {
                mDatabind.et3.requestFocus()
            } else {
                mDatabind.et1.requestFocus()
            }
        }
        mDatabind.et3.addTextChangedListener {
            if (it?.length != 0) {
                mDatabind.et4.requestFocus()
            } else {
                mDatabind.et2.requestFocus()
            }
        }
        mDatabind.et4.addTextChangedListener {
            if (it?.length != 0) {
                mViewModel.checkCode(mobile,"${mDatabind.et1.text}${mDatabind.et2.text}${mDatabind.et3.text}${mDatabind.et4.text}")

            } else {
                mDatabind.et3.requestFocus()
            }
        }
        mDatabind.et2.setOnKeyListener { p0, p1, event ->
            if (event?.keyCode == KeyEvent.KEYCODE_DEL && event?.action == KeyEvent.ACTION_DOWN) {
                mDatabind.et1.requestFocus()
            }
            false
        }
        mDatabind.et3.setOnKeyListener { p0, p1, event ->
            if (event?.keyCode == KeyEvent.KEYCODE_DEL && event?.action == KeyEvent.ACTION_DOWN) {
                mDatabind.et2.requestFocus()
            }
            false
        }
        mDatabind.et4.setOnKeyListener { p0, p1, event ->
            if (event?.keyCode == KeyEvent.KEYCODE_DEL && event?.action == KeyEvent.ACTION_DOWN) {
                mDatabind.et3.requestFocus()
            }
            false
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.codeResult.observe(this){
            parseState(it,{
                println(it)
            },{
                showToast("验证码获取失败")
            })
        }

        mViewModel.checkCodeResult.observe(this){
            parseState(it,{
                println(it)
                CacheUtil.setUser(userInfo)
                App.userInfo = userInfo
                startActivity(Intent(this@VerificationCodeActivity, MainActivity::class.java))
                finish()
            },{
                showToast("验证码错误")
            })
        }
    }
    private fun startCountDown() {
        mViewModel.verificationCodeisEnabled.set(false)
        mDatabind.countDownTv.setTextColor(ContextCompat.getColor(appContext, R.color.hg_text_gray))
        object : CountDownTimer(60000, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                mViewModel.verificationCodeText.set(String.format("%s秒后重新发送", millisUntilFinished / 1000))
            }

            override fun onFinish() {
                mViewModel.verificationCodeText.set("发送验证码")
                mDatabind.countDownTv.setTextColor(ContextCompat.getColor(appContext, R.color.colorAccent))
                mViewModel.verificationCodeisEnabled.set(true)
            }
        }.start()
    }

    inner class ClickProxy {
        fun clickBack() {
            finish()
        }

        fun clickGetVerificationCode() {
            startCountDown()
            mViewModel.getCode(mobile)
        }
    }

}